package com.yokit.resource_management.framework.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yokit.resource_management.dto.ResponseDto;
import com.yokit.resource_management.entity.Car;
import com.yokit.resource_management.framework.service.CarService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fengzeng
 * @create 2021/6/25 3:00
 */
@RestController
public class CarController {

    @Resource
    private CarService carService;

    /**
     * 获取所有车辆信息，分页进行输出
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/carList", method = RequestMethod.GET)
    public Map getCarList(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Car> cars = carService.selectAll();
        PageInfo pageInfo = PageInfo.of(cars);

        Map map = new HashMap();
        map.put("status", 200);
        map.put("msg", "获取车辆列表成功");
        map.put("data", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return map;
    }

    /**
     * 按名称模糊查找车辆
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/searchCar", method = RequestMethod.GET)
    public Map getCarByName(@RequestParam(value = "search") String search, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Car> cars = carService.selectCarByName(search);
        PageInfo pageInfo = PageInfo.of(cars);
        Map map = new HashMap();
        map.put("data", pageInfo.getList());
        map.put("status", 200);
        map.put("msg", "查询车辆成功");
        map.put("total", pageInfo.getTotal());
        return map;
    }

    /**
     * 更新车辆状态
     *
     * @param carId
     * @param checked
     * @return
     */
    @PutMapping("/car/{carId}/{checked}")
    public ResponseDto updateCarStatus(@PathVariable String carId, @PathVariable String checked) {
        Integer carId_ = Integer.parseInt(carId);
        Car car = new Car();
        car.setCarId(carId_);
        car.setIsActive(checked);
        carService.updateCarStatus(car);
        return new ResponseDto(200, "更新数据成功");
    }

    /**
     * @return
     * @Description 添加车辆
     * @Date ${time} ${date}
     * @Param
     **/
    @RequestMapping(value = "/addcar", method = RequestMethod.POST)
    public ResponseDto addCar(@RequestBody Map<String, Object> addCar) {
        String carName = addCar.get("carName").toString();

        //前端传回的参数类型是[xxx,xxx,xxx],处理一下去掉'[' ']'
        String carDesc = addCar.get("carDesc").toString();
        carDesc = StringUtils.remove(carDesc, "[");
        carDesc = StringUtils.remove(carDesc, "]");
        Car car = new Car();
        if (!ObjectUtils.isEmpty(addCar.get("carPic"))) {
            String carPic = addCar.get("carPic").toString();
            car.setCarPic("/image/" + carPic);
        }

        car.setCarDesc(carDesc);
        car.setCarName(carName);
        car.setResourceId(101);
        carService.insertCar(car);
        return new ResponseDto(200, "添加车辆成功");

    }

    /**
     * @return
     * @Description 删除车辆
     * @Date
     * @Param
     **/
    @DeleteMapping("/car/{carId}")
    public ResponseDto deleteCar(@PathVariable String carId) {
        Integer carId_ = Integer.parseInt(carId);
        carService.deleteCar(carId_);
        return new ResponseDto(200, "删除成功");

    }

    /**
     * 根据车辆id 更新车辆信息
     *
     * @param carId
     * @param request
     * @return
     */
    @PutMapping("/car/{carId}")
    public ResponseDto updateCar(@PathVariable String carId, @RequestBody Map<String, Object> request) {

        Car car = new Car();
        Integer carId_ = Integer.parseInt(carId);
        String carName = request.get("carName").toString();

        //前端传回的参数类型是[xxx,xxx,xxx],处理一下去掉'[' ']'
        String carDesc = request.get("carDesc").toString();
        carDesc = StringUtils.remove(carDesc, "[");
        carDesc = StringUtils.remove(carDesc, "]");

        if (request.get("carPic") != null) {
            String carPic = request.get("carPic").toString();
            car.setCarPic(carPic);
        }


        car.setCarName(carName);
        car.setCarId(carId_);

        car.setCarDesc(carDesc);

        carService.updateCar(car);

        return new ResponseDto(200, "操作成功");
    }

}

